package com.example.androidlearning2200.utils;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 与SQLite数据库相关的工具类
 */
public class SQLUtil {
    /**
     * 判断表是否存在
     *
     * @param db
     * @param tableName
     * @return
     */
    public static boolean doesTableExist(SQLiteDatabase db, String tableName) {
        Cursor cursor = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='" + tableName + "'", null);
        if (cursor != null) {
            return cursor.moveToFirst();
        }
        return false;
    }

    /**
     * 清空表数据
     *
     * @param db
     * @param tableName
     */
    public static void clearTable(SQLiteDatabase db, String tableName) {
        // 清空数据表
        String sqlDelete = "delete from " + tableName;
        db.execSQL(sqlDelete);
    }
}
